<!DOCTYPE html>
<html lang="zh-CN">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="robots" content="noodp" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1">
        <title>《Flutter开发Tip2》 - 飞雪无情的博客</title><meta name="Description" content="专注于IT互联网，包括但不限于Go语言(golang)、Java、Android、Python、项目管理、抖音分析、软件架构等"><meta property="og:title" content="《Flutter开发Tip2》" />
<meta property="og:description" content="
本文主要介绍flutter开发小技巧
" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://www.flysnow.org/posts/flutter/flutter%E5%BC%80%E5%8F%91Tip2/" />
<meta property="article:published_time" content="2021-07-30T22:43:19+08:00" />
<meta property="article:modified_time" content="2021-07-30T22:43:19+08:00" />
<meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="《Flutter开发Tip2》"/>
<meta name="twitter:description" content="
本文主要介绍flutter开发小技巧
"/>
<meta name="application-name" content="飞雪无情的博客">
<meta name="apple-mobile-web-app-title" content="飞雪无情的博客"><link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" />
        <link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
        <link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png"><link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png"><link rel="manifest" href="/site.webmanifest"><link rel="canonical" href="https://www.flysnow.org/posts/flutter/flutter%E5%BC%80%E5%8F%91Tip2/" /><link rel="prev" href="https://www.flysnow.org/posts/flutter/flutter%E5%BC%80%E5%8F%91Tip1/" /><link rel="next" href="https://www.flysnow.org/posts/flutter/flutter%E5%BC%80%E5%8F%91Tip3/" /><link rel="stylesheet" href="/lib/normalize/normalize.min.css"><link rel="stylesheet" href="/css/style.min.css"><link rel="stylesheet" href="/lib/fontawesome-free/all.min.css"><link rel="stylesheet" href="/lib/animate/animate.min.css"><script type="application/ld+json">
    {
        "@context": "http://schema.org",
        "@type": "BlogPosting",
        "headline": "《Flutter开发Tip2》",
        "inLanguage": "zh-CN",
        "mainEntityOfPage": {
            "@type": "WebPage",
            "@id": "https:\/\/www.flysnow.org\/posts\/flutter\/flutter%E5%BC%80%E5%8F%91Tip2\/"
        },"genre": "posts","keywords": "flutter","wordcount":  770 ,
        "url": "https:\/\/www.flysnow.org\/posts\/flutter\/flutter%E5%BC%80%E5%8F%91Tip2\/","datePublished": "2021-07-30T22:43:19+08:00","dateModified": "2021-07-30T22:43:19+08:00","publisher": {
            "@type": "Organization",
            "name": "飞雪无情"},"author": {
                "@type": "Person",
                "name": "飞雪无情"
            },"description": ""
    }
    </script></head>
    <body header-desktop="" header-mobile=""><script type="text/javascript">(window.localStorage && localStorage.getItem('theme') ? localStorage.getItem('theme') === 'dark' : ('' === 'auto' ? window.matchMedia('(prefers-color-scheme: dark)').matches : '' === 'dark')) && document.body.setAttribute('theme', 'dark');</script>

        <div id="mask"></div><div class="wrapper"><div class="logo-wrapper">
  <a href="/%20/" class="logo">飞雪无情的博客</a>
</div>

<nav class="site-navbar">
  <ul id="menu" class="menu">
    <li class="menu-item">
      <a class="menu-item-link" href="/tools/">工具</a>
    </li><li class="menu-item">
      <a class="menu-item-link" href="/archives/">归档</a>
    </li><li class="menu-item">
      <a class="menu-item-link" href="/about/">关于</a>
    </li>
  </ul>
</nav><main class="main">
                <div class="container"><div class="toc" id="toc-auto">
            <h2 class="toc-title">Contents</h2>
            <div class="toc-content" id="toc-content-auto"></div>
        </div><article class="page single"><h1 class="single-title animated flipInX">《Flutter开发Tip2》</h1><div class="post-meta">
            <div class="post-meta-line"><span class="post-author"><a href="/" title="Author" rel=" author" class="author"><i class="fas fa-user-circle fa-fw"></i>飞雪无情</a></span>&nbsp;<span class="post-category">included in <a href="/categories/Flutter/"><i class="far fa-folder fa-fw"></i>Flutter</a></span></div>
            <div class="post-meta-line"><i class="far fa-calendar-alt fa-fw"></i>&nbsp;<time datetime="2021-07-30">2021-07-30</time>&nbsp;<i class="fas fa-pencil-alt fa-fw"></i>&nbsp;770 words&nbsp;
                <i class="far fa-clock fa-fw"></i>&nbsp;2 minutes&nbsp;</div>
        </div><div class="featured-image"><img
        class="lazyload"
        src="/svg/loading.min.svg"
        data-src="https://luckly007.oss-cn-beijing.aliyuncs.com/img/90c6cc12-742e-4c9f-b318-b912f163b8d0.png"
        data-srcset="https://luckly007.oss-cn-beijing.aliyuncs.com/img/90c6cc12-742e-4c9f-b318-b912f163b8d0.png, https://luckly007.oss-cn-beijing.aliyuncs.com/img/90c6cc12-742e-4c9f-b318-b912f163b8d0.png 1.5x, https://luckly007.oss-cn-beijing.aliyuncs.com/img/90c6cc12-742e-4c9f-b318-b912f163b8d0.png 2x"
        data-sizes="auto"
        alt="https://luckly007.oss-cn-beijing.aliyuncs.com/img/90c6cc12-742e-4c9f-b318-b912f163b8d0.png"
        title="https://luckly007.oss-cn-beijing.aliyuncs.com/img/90c6cc12-742e-4c9f-b318-b912f163b8d0.png" /></div><div class="details toc" id="toc-static"  kept="">
                <div class="details-summary toc-title">
                    <span>Contents</span>
                    <span><i class="details-icon fas fa-angle-right"></i></span>
                </div>
                <div class="details-content toc-content" id="toc-content-static"><nav id="TableOfContents">
  <ul>
    <li><a href="#代码静态分析">代码静态分析</a>
      <ul>
        <li><a href="#flutter-analyze">flutter analyze</a></li>
        <li><a href="#dartfmt">dartfmt</a></li>
      </ul>
    </li>
    <li><a href="#push--present--pop">Push &amp; present &amp; Pop</a></li>
    <li><a href="#获取widget的位置和宽高">获取widget的位置和宽高</a></li>
    <li><a href="#model-to-json">Model To JSON</a>
      <ul>
        <li><a href="#json_serializable">json_serializable</a></li>
        <li><a href="#built-value">Built value</a></li>
      </ul>
    </li>
    <li><a href="#参考">参考</a></li>
  </ul>
</nav></div>
            </div><div class="content" id="content"><blockquote>
<p>本文主要介绍flutter开发小技巧</p>
</blockquote>
<h2 id="代码静态分析">代码静态分析</h2>
<p>在提交代码时为了提高代码质量，保持团队的代码风格一致，需要进行代码静态分析，一般通过下面2种方法来进行</p>
<h3 id="flutter-analyze">flutter analyze</h3>
<p>使用<code>flutter analyze</code>进行代码静态分析,此命令会根据<code>analysis_options.yaml</code>定义的规则进行静态分析</p>
<h3 id="dartfmt">dartfmt</h3>
<p>使用<code>dartfmt ./ -w</code>对当前目录以及子目录的dart代码进行代码,<code>-w</code>选项会自动重写文件使其符合规范。</p>
<p>使用<code>dartfmt ./ -n</code>显示当前目录以及子目录的dart代码格式可以修改的文件但是不做修改，可以配合ci分析代码格式问题。</p>
<p>更多选项请使用<code>dartfmt --help</code>查看</p>
<h2 id="push--present--pop">Push &amp; present &amp; Pop</h2>
<pre><code>// Push by route
Navigator.pushNamed(context, '/b')
  
// push 
Navigator.of(context).push(MaterialPageRoute(builder: (BuildContext context) =&gt; MyPage()));

// present
Navigator.of(context).push(MaterialPageRoute(builder: (BuildContext context) =&gt; MyPage(),fullscreenDialog: true));

// pop
Navigator.pop(context)
  
// pop + push
Navigator.of(context)
	..pop()
	..pop()
	..pushNamed('/settings');
</code></pre><p>API</p>
<pre><code>// push
Future push(BuildContext context, Route route)

// pop
bool pop(BuildContext context, [ result ])
  
// 下面两种是等效的
Navigator.push(BuildContext context, Route route)
Navigator.of(context).push(Route route)
</code></pre><h2 id="获取widget的位置和宽高">获取widget的位置和宽高</h2>
<pre><code>final RenderBox box = keyContext.findRenderObject();
final size = box.size;
final topLeftPosition = box.localToGlobal(Offset.zero);
</code></pre><p>See more</p>
<ul>
<li><a href="https://stackoverflow.com/questions/49307677/how-to-get-a-height-of-a-widget" target="_blank" rel="noopener noreffer">How to get a height of a Widget?</a></li>
<li><a href="https://medium.com/@diegoveloper/flutter-widget-size-and-position-b0a9ffed9407" target="_blank" rel="noopener noreffer">Flutter : Widget Size and Position</a></li>
</ul>
<h2 id="model-to-json">Model To JSON</h2>
<h3 id="json_serializable">json_serializable</h3>
<ol>
<li>
<p>引入</p>
<pre><code>dependencies:
  # Your other regular dependencies here
  json_annotation: ^2.0.0
   
dev_dependencies:
  # Your other dev_dependencies here
  build_runner: ^1.0.0
  json_serializable: ^2.0.0
</code></pre></li>
<li>
<p>使用</p>
<pre><code>import 'package:json_annotation/json_annotation.dart';
   
// user.g.dart 将在我们运行生成命令后自动生成
part 'user.g.dart';
   
///这个标注是告诉生成器，这个类是需要生成Model类的
@JsonSerializable()
   
class User{
  User(this.name, this.email);
   
  String name;
  String email;
  //不同的类使用不同的mixin即可
  factory User.fromJson(Map&lt;String, dynamic&gt; json) =&gt; _$UserFromJson(json);
  Map&lt;String, dynamic&gt; toJson() =&gt; _$UserToJson(this);
}
</code></pre></li>
<li>
<p>单次解析：<code>flutter packages pub run build_runner build</code></p>
</li>
<li>
<p>持续集成：<code>flutter packages pub run build_runner watch</code></p>
</li>
<li>
<p><a href="https://caijinglong.github.io/json2dart/index_ch.html" target="_blank" rel="noopener noreffer">json_serializable 在线json转dart model工具</a></p>
</li>
</ol>
<h3 id="built-value">Built value</h3>
<ol>
<li>
<p>在线json转build value 模板工具 <a href="https://charafau.github.io/json2builtvalue/">https://charafau.github.io/json2builtvalue/</a></p>
</li>
<li>
<p><a href="https://marketplace.visualstudio.com/items?itemName=GiancarloCode.built-value-snippets" target="_blank" rel="noopener noreffer">VSCode built value 插件</a></p>
</li>
<li>
<p>Sample code</p>
<pre><code>// interface model
import 'package:built_value/built_value.dart';
   
part 'user.g.dart';
   
abstract class User implements Built&lt;User, UserBuilder&gt; {
  String get name;
  @nullable
  String get nickname;
  User._();
  factory User([updates(UserBuilder b)]) = _$User;
}
   
// init
var user1 = new User((b) =&gt; b
    ..name = 'John Smith'
    ..nickname = 'Joe');
   
// update
var user2 = user.rebuild((b) =&gt; b
    ..nickname = 'Jojo');
   
// update
var updatedStructuredData = structuredData.rebuild((b) =&gt; b
    ..user.update((b) =&gt; b
        ..name = 'Johnathan Smith')
    ..credentials.phone.update((b) =&gt; b
        ..country = Country.switzerland
        ..number = '555 01234 555'));
   
   
// nested builders
abstract class Node implements Built&lt;Node, NodeBuilder&gt; {
  @nullable
  String get label;
  @nullable
  Node get left; 
  @nullable
  Node get right;
  Node._();
  factory Node([updates(NodeBuilder b)]) = _$Node;
}
   
// new or update
var node = new Node((b) =&gt; b
  ..left.left.left.right.left.right.label = 'I’m a leaf!'
  ..left.left.right.right.label = 'I’m also a leaf!');
var updatedNode = node.rebuild((b) =&gt; b
  ..left.left.right.right.label = 'I’m not a leaf any more!'
  ..left.left.right.right.right.label = 'I’m the leaf now!');
</code></pre></li>
</ol>
<h2 id="参考">参考</h2>
<ul>
<li><a href="https://medium.com/dartlang/3-cool-dart-patterns-6d8d9d3d8fb8" target="_blank" rel="noopener noreffer">3 cool Dart patterns</a></li>
<li><a href="https://pub.dev/packages/built_collection" target="_blank" rel="noopener noreffer">built_collection - pub.dev</a></li>
<li><a href="https://medium.com/dartlang/darts-built-collection-for-immutable-collections-db662f705eff" target="_blank" rel="noopener noreffer">Dart’s built_collection for Immutable Collections</a></li>
<li><a href="https://dart.dev/tools/dartfmt" target="_blank" rel="noopener noreffer">dartfmt</a></li>
<li><a href="https://dart.dev/guides/language/analysis-options" target="_blank" rel="noopener noreffer">Customizing static analysis</a></li>
</ul></div><div class="post-footer" id="post-footer">
    <div class="post-info">
        <div class="post-info-line">
            <div class="post-info-mod">
                <span>Updated on 2021-07-30</span>
            </div>
            <div class="post-info-license"></div>
        </div>
        <div class="post-info-line">
            <div class="post-info-md"></div>
            <div class="post-info-share">
                <span></span>
            </div>
        </div>
    </div>

    <div class="post-info-more">
        <section class="post-tags"><i class="fas fa-tags fa-fw"></i>&nbsp;<a href="/tags/flutter/">flutter</a></section>
        <section>
            <span><a href="javascript:void(0);" onclick="window.history.back();">Back</a></span>&nbsp;|&nbsp;<span><a href="/">Home</a></span>
        </section>
    </div>

    <div class="post-nav"><a href="/posts/flutter/flutter%E5%BC%80%E5%8F%91Tip1/" class="prev" rel="prev" title="《Flutter开发Tip1》"><i class="fas fa-angle-left fa-fw"></i>《Flutter开发Tip1》</a>
            <a href="/posts/flutter/flutter%E5%BC%80%E5%8F%91Tip3/" class="next" rel="next" title="《Flutter开发Tip3》">《Flutter开发Tip3》<i class="fas fa-angle-right fa-fw"></i></a></div>
</div>
</article></div>
            </main><footer class="footer">
        <div class="footer-container"><div class="footer-line">Powered by <a href="https://gohugo.io/" target="_blank" rel="noopener noreffer" title="Hugo 0.79.1">Hugo</a> | Theme - <a href="https://github.com/dillonzq/LoveIt" target="_blank" rel="noopener noreffer" title="LoveIt 0.2.10"><i class="far fa-kiss-wink-heart fa-fw"></i> LoveIt</a>
                </div><div class="footer-line"><i class="far fa-copyright fa-fw"></i><span itemprop="copyrightYear">2021</span><span class="author" itemprop="copyrightHolder">&nbsp;<a href="/" target="_blank">飞雪无情</a></span></div>
        </div>
    </footer></div>

        <div id="fixed-buttons"><a href="#" id="back-to-top" class="fixed-button" title="Back to Top">
                <i class="fas fa-arrow-up fa-fw"></i>
            </a><a href="#" id="view-comments" class="fixed-button" title="View Comments">
                <i class="fas fa-comment fa-fw"></i>
            </a>
        </div><script type="text/javascript" src="/lib/smooth-scroll/smooth-scroll.min.js"></script><script type="text/javascript" src="/lib/lazysizes/lazysizes.min.js"></script><script type="text/javascript" src="/lib/clipboard/clipboard.min.js"></script><script type="text/javascript" src="/lib/sharer/sharer.min.js"></script><script type="text/javascript">window.config={"code":{"copyTitle":"Copy to clipboard","maxShownLines":10},"comment":{}};</script><script type="text/javascript" src="/js/theme.min.js"></script></body>
</html>
